#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/select.h>
#include <sys/time.h>
#include <errno.h>

#define TEST_MAGIC 'x' // 定义幻数

#define LED1 _IO(TEST_MAGIC, 0)
#define LED2 _IO(TEST_MAGIC, 1)
#define LED3 _IO(TEST_MAGIC, 2)
#define LED4 _IO(TEST_MAGIC, 3)

int main(int argc, char **argv)
{
	int led_fd;
	char led_val[4] = {0}; // LED灯的状态

	led_fd = open("/dev/Led", O_RDWR); // 打开设备下的LED，成功返回0
	if (led_fd < 0)
	{
		perror("Can not open /dev/LED\n");
		return 0;
	}
	int i = 1;
	while (1)
	{
		switch (i)
		{
		case 0:
			// 翻转灯的状态
			if (led_val[0])
			{
				ioctl(led_fd, LED1, 1);
				led_val[0] = 0;
			}
			else
			{
				ioctl(led_fd, LED1, 0);
				led_val[0] = 1;
			}
			break;
		case 1:
			if (led_val[1])
			{
				ioctl(led_fd, LED2, 1);
				led_val[1] = 0;
			}
			else
			{
				ioctl(led_fd, LED2, 0);
				led_val[1] = 1;
			}
			break;
		case 2:
			if (led_val[2])
			{
				ioctl(led_fd, LED3, 1);
				led_val[2] = 0;
			}
			else
			{
				ioctl(led_fd, LED3, 0);
				led_val[2] = 1;
			}
			break;
		case 3:
			if (led_val[3])
			{
				ioctl(led_fd, LED4, 1);
				led_val[3] = 0;
			}
			else
			{
				ioctl(led_fd, LED4, 0);
				led_val[3] = 1;
			}
			break;
		default:
			break;
		}
		i++;
		i %= 4;
		sleep(1);
	}
	return 0;
}
